﻿#include "TradeOrderIO.h"
#include "OptionManager.h"
#include "json/json.h"
#include "TradeDetailJson.h"
#include "JsonTools.h"

TradeOrderIO::TradeOrderIO()
{
}


TradeOrderIO::~TradeOrderIO()
{
}

void TradeOrderIO::Save(const std::map<std::string, TD::Order> &pOrderInfo)
{
	std::string orderFile = OptionManager::Instance().GetOrderFile();
	Json::Value orderRoot;
	for (std::map<std::string, TD::Order>::const_iterator it = pOrderInfo.begin(); it != pOrderInfo.end(); ++it)
	{
		Json::Value &pOrder = orderRoot[it->first] = Json::Value();
		TDJson::TradeOrderToJson(pOrder, it->second);
	}
	JsonTool::JsonToFile(orderRoot, orderFile);
}

void TradeOrderIO::Read(std::map<std::string, TD::Order> &outOrderInfo)
{
	std::string orderFile = OptionManager::Instance().GetOrderFile();
	Json::Value orderRoot;
	JsonTool::FileToJson(orderFile, orderRoot);
	for (Json::Value::iterator it = orderRoot.begin(); it != orderRoot.end(); ++it)
	{
		const char *pname = it.name().c_str();
		TD::Order &pOrder = outOrderInfo[pname] = TD::Order();
		TDJson::JsonToTradeOrder(pOrder, *it);
	}
}

bool TradeOrderIO::IsHasOrder(const std::string &pSymbol)
{
	std::map<std::string, TD::Order> pOrderInfo;
	Read(pOrderInfo);
	if (pOrderInfo.empty())
	{
		return false;
	}
	return pOrderInfo.find(pSymbol) != pOrderInfo.end();
}
